package com.tenbent.product.center.insures.action;

import com.tenbent.product.base.action.BaseController;
import com.tenbent.product.base.constants.WebConstans;
import com.tenbent.product.base.query.page.PageResult;
import com.tenbent.product.base.web.Page;
import com.tenbent.product.center.insures.bo.SafeguardPackageDetail;
import com.tenbent.product.center.insures.dto.SafeguardPackageDetailDto;
import com.tenbent.product.center.insures.dto.search.SafeguardPackageDetailSearchPageDto;
import com.tenbent.product.center.insures.service.SafeguardPackageDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 保障套餐明细控制器
 *
 * @author Randy
 *
 *         Created by ThinkPad on 2017/8/29.
 */
@RestController
public class SafeguardPackageDetailController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SafeguardPackageDetailController.class);

	public static class Url {
		/** 添加保障套餐 */
		public static final String save = "/api/safeguardPackageDetail/save";
		/** 修改保障套餐 */
		public static final String update = "/api/safeguardPackageDetail/update";
		/** 删除保障套餐 */
		public static final String delete = "/api/safeguardPackageDetail/delete/{id}";
		/** 根据套餐code查询 */
		public static final String queryByCode = "/api/safeguardPackageDetail/query_by_code";
		/** 保障套餐查询 */
		public static final String query = "/api/safeguardPackageDetail/query";
	}

	@Autowired
	@Qualifier("safeguardPackageDetailServiceImpl")
	private SafeguardPackageDetailService safeguardPackageDetailService;

	@RequestMapping(value = Url.save, method = { RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseStatus(HttpStatus.CREATED)
	public SafeguardPackageDetailDto saveApi(@Valid @RequestBody SafeguardPackageDetailDto safeguardPackageDetailDto,
			Errors errors, @ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {

		processErrors(errors);
		settingSaveManager(safeguardPackageDetailDto, managerId);
		SafeguardPackageDetail safeguardPackageDetail = safeguardPackageDetailDto.transToBo();
		safeguardPackageDetailService.add(safeguardPackageDetail);
		return new SafeguardPackageDetailDto(safeguardPackageDetail);
	}

	@RequestMapping(value = Url.update, method = {
			RequestMethod.PUT }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public SafeguardPackageDetailDto updateApi(@Valid @RequestBody SafeguardPackageDetailDto safeguardPackageDetailDto,
			Errors errors, @ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {

		if (safeguardPackageDetailDto.getId() == null) {
			errors.rejectValue("id", "safeguard.package.detail.id.not.empty",
					getMessage("safeguard.package.detail.id.not.empty", null));
		}
		processErrors(errors);
		settingModiferManager(safeguardPackageDetailDto, managerId);
		SafeguardPackageDetail safeguardPackageDetail = safeguardPackageDetailDto.transToBo();
		safeguardPackageDetailService.update(safeguardPackageDetail);
		return new SafeguardPackageDetailDto(safeguardPackageDetail);
	}

	@RequestMapping(value = Url.delete, method = {
			RequestMethod.DELETE }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void deleteApi(@PathVariable Integer id, @ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {
		safeguardPackageDetailService.deleteLogic(id, managerId);
	}

	@RequestMapping(value = Url.query, method = { RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Page<SafeguardPackageDetailDto> query(SafeguardPackageDetailSearchPageDto searchPageDto) {

		PageResult<SafeguardPackageDetail> pageResult = safeguardPackageDetailService
				.queryPageObj(searchPageDto.transToPageable());
		return new Page(change.changesToDTOPage(pageResult, SafeguardPackageDetailDto.class));
	}

	@RequestMapping(value = Url.queryByCode, method = {
			RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public List<SafeguardPackageDetailDto> queryByPackageCode(@RequestParam("packageCode") String packageCode,
			@RequestParam("categoryCode") String categoryCode) {

		SafeguardPackageDetail safeguardPackageDetail = new SafeguardPackageDetail();
		safeguardPackageDetail.setPackageCode(packageCode);
		safeguardPackageDetail.setCategoryCode(categoryCode);
		List<SafeguardPackageDetail> list = safeguardPackageDetailService.queryObj(safeguardPackageDetail);
		return change.changesToDTOList(list, SafeguardPackageDetailDto.class);
	}
}
